import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:schoollifedemo/business/school/viewmodel/announcement_see_more_page_viewmodel.dart';
import 'package:schoollifedemo/commonwidget/circle_empty_widget.dart';
import 'package:schoollifedemo/commonwidget/common_base_page.dart';
import 'package:schoollifedemo/commonwidget/common_dialog.dart';
import 'package:schoollifedemo/commonwidget/error_widget.dart';
import 'package:schoollifedemo/commonwidget/loading_widget.dart';
import 'package:schoollifedemo/commonwidget/text_widget_with_overflow.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class AnnouncementSeeMorePage extends BaseStatefulPage {
  final String? mainTitleMd5;

  const AnnouncementSeeMorePage({
    Key? key,
    this.mainTitleMd5,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _AnnouncementSeeMorePageState();
  }
}

class _AnnouncementSeeMorePageState
    extends BasePageState<AnnouncementSeeMorePage> {
  final AnnouncementSeeMorePageViewModel _viewModel =
      AnnouncementSeeMorePageViewModel();

  @override
  void initState() {
    _viewModel.init();
    _viewModel.pullSchoolAnnouncement(
      onSuccess: () {
        _viewModel.flutterTitle(
          mainTitleMd5: widget.mainTitleMd5,
        );
      },
      onFailure: (msg) {
        DialogUtils.showTitleAndMessageDialog(
          context,
          clickOutSideDismiss: true,
          title: "错误",
          msg: "$msg",
        );
      },
    );
    super.initState();
  }

  @override
  void dispose() {
    _viewModel.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(
                left: 10,
                right: 10,
                top: MediaQuery.of(context).padding.top,
              ),
              child: Row(
                children: [
                  GestureDetector(
                    onTap: () {
                      _viewModel.backPage(context);
                    },
                    child: Container(
                      margin: const EdgeInsets.only(left: 15),
                      child: const Icon(Icons.arrow_back_ios_outlined),
                    ),
                  ),
                  const Expanded(
                    child: TextWidgetWithOverflow(
                      '学校公告',
                      fontSize: 28,
                      insideColor: Colors.transparent,
                      textAlign: TextAlign.left,
                    ),
                  ),
                ],
              ),
            ),
            StreamBuilder(
              stream: _viewModel.schoolAnnouncementLoadingState,
              initialData: CommonLoadingState.loading,
              builder: (buildContext, snapshot) {
                final state = snapshot.data as CommonLoadingState?;
                switch (state) {
                  case CommonLoadingState.loading:
                    return Expanded(child: _loadingWidget());
                  case CommonLoadingState.successTypeOne:
                  case CommonLoadingState.successTypeTwo:
                    return Expanded(child: _commonBody());
                  default:
                    return Expanded(child: _errorWidget());
                }
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _commonBody() {
    return RefreshIndicator(
      onRefresh: () async {
        // refresh
        _viewModel.pullSchoolAnnouncement();
      },
      child: ListView(
        physics: const BouncingScrollPhysics(),
        children: [
          Container(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 30),
            child: Column(
              children: [
                ListView.separated(
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (buildContext, mainIndex) {
                    final announcementListItem =
                        _viewModel.filterResp?.announcementList?[mainIndex];
                    return Column(
                      children: [
                        Container(
                          margin: const EdgeInsets.only(bottom: 5),
                          child: TextWidgetWithOverflow(
                            announcementListItem?.mainTitle,
                            fontSize: 26,
                            insideColor: Colors.transparent,
                            textAlign: TextAlign.center,
                          ),
                        ),
                        ListView.separated(
                          physics: const NeverScrollableScrollPhysics(),
                          shrinkWrap: true,
                          itemBuilder: (buildContext, index) {
                            final contentListItem = _viewModel
                                .filterResp
                                ?.announcementList?[mainIndex]
                                ?.contentList?[index];
                            return _itemWidget(
                              mainTitle: _viewModel.filterResp
                                  ?.announcementList?[mainIndex]?.mainTitle,
                              content: contentListItem?.content ?? '',
                              time: contentListItem?.time ?? '',
                              url: contentListItem?.url ?? '',
                            );
                          },
                          separatorBuilder: (buildContext, index) {
                            return Container(
                              height: 10,
                            );
                          },
                          itemCount: 5,
                        ),
                      ],
                    );
                  },
                  separatorBuilder: (buildContext, index) {
                    return Container(
                      height: 10,
                    );
                  },
                  itemCount:
                      _viewModel.filterResp?.announcementList?.length ?? 0,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _itemWidget({
    String? mainTitle,
    required String content,
    required String time,
    required String url,
  }) {
    return GestureDetector(
      onTap: () {
        // 跳转页面，然后在另外的页面拉取信息
        _viewModel.turnToDetailPage(
          context,
          url: url,
          mainTitle: mainTitle,
        );
      },
      child: CircleEmptyWidget(
        Column(
          children: [
            Container(
              margin: const EdgeInsets.only(left: 20, right: 20, top: 5),
              child: TextWidgetWithOverflow(
                content,
                fontSize: 18,
                insideColor: Colors.transparent,
                textAlign: TextAlign.left,
                width: double.infinity,
              ),
            ),
            Container(
              margin: const EdgeInsets.only(left: 20, right: 20, bottom: 5),
              child: TextWidgetWithOverflow(
                time,
                fontSize: 15,
                insideColor: Colors.transparent,
                textAlign: TextAlign.left,
                width: double.infinity,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _loadingWidget() {
    return Center(
      child: Container(
        height: 50,
        color: Colors.white,
        child: const LoadingWidget(),
      ),
    );
  }

  Widget _errorWidget() {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: CommonErrorWidget(
          '错误重试',
          onButtonClick: () async {
            _viewModel.pullSchoolAnnouncement();
          },
        ),
      ),
    );
  }
}
